package com.lb.shopguide.ui.fragment.boss;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.http.ResponseCode;
import com.lb.baselib.http.SimpleLbObserver;
import com.lb.baselib.http.bean.BaseResponse;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.http.method.ApiMethodBoss;
import com.lb.baselib.http.SimpleObserverOnNextListener;
import com.lb.baselib.http.SimpleObserverOtherListener;
import com.lb.shopguide.ui.view.ClearTitleBar;
import com.lb.shopguide.util.lb.BizUtil;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * Created by kang on 2017/6/23.
 */

public class FragmentFeedback extends BaseCommonFragment {
    @BindView(R.id.ctb)
    ClearTitleBar ctb;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;

    public static FragmentFeedback newInstance() {
        return new FragmentFeedback();
    }



    @Override
    public int getLayoutId() {
        return R.layout.fragment_feedback;
    }

    @Override
    public void initData() {
        mUserConfigManager = UserConfigManager.getInstance(mContext);
    }

    @Override
    public void initView() {
        ctb.setLeftImageSrc(R.drawable.iv_back);
        ctb.setTvTitle("意见反馈");
        ctb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    @Override
    public String getPageTag() {
        return FragmentFeedback.class.getName();
    }

    private void sendRequestFeedback(String content) {
        HashMap hashMap = new HashMap();
        hashMap.put("appEntranceType", AppConstant.APP_ENTRANCE_ANDROID);
        hashMap.put("feedbackContent", content);
        RequestBody body = BizUtil.getBodyFromMap(hashMap);
        SimpleObserverOnNextListener<BaseResponse> listener = new SimpleObserverOnNextListener<BaseResponse>() {

            @Override
            public void onNext(BaseResponse response) {
                if (response.getReturnCode() == (ResponseCode.RESPONSE_SUCCESS)) {
                    ToastUtils.showShort("意见反馈成功");
                    pop();
                }
            }
        };
        SimpleObserverOtherListener errorListener = new SimpleObserverOtherListener() {
            @Override
            public void onSessionOutOfDate() {

            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort("意见反馈失败");
            }
        };
        ApiMethodBoss.bossFeedback(new SimpleLbObserver(listener, errorListener), body,this, FragmentEvent.DESTROY_VIEW);
    }

    @OnClick(R.id.tv_submit)
    void submit() {
        String content = etContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showShort("请输入反馈意见");
            return;
        } else {
            sendRequestFeedback(content);
        }
    }

}
